<template>
  <el-dialog width="66%" :title="dialogTitle" v-model="dialogVisible" modal @close="closeDialog" draggable
    :close-on-click-modal="false" :append-to-body="true">
    <el-row>
      <el-col :span="12">
        <el-form class="dialog-form" :model="formData" ref="vForm" label-position="right" label-width="80px"
          size="medium" @submit.native.prevent :rules="formRules" :disabled="formDisabled">
          <el-form-item label="异常类型" prop="exceptionType">
            <el-select v-model="formData.exceptionType" placeholder="请选择异常类型" class="full-width-input" clearable>
              <el-option v-for="(item, index) in exceptionTypeOptions" :key="index" :label="item.label"
                :value="item.value" :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-table :data="tableData" border style="width: 100%" height="30vh">
      <el-table-column v-for="item of tableProps" :key="item.props" :prop="item.prop" :label="item.title"
        :width="item.width">
      </el-table-column>
      <el-table-column label="操作" min-width="60">
        <template slot-scope="{ row }">
          <el-popconfirm title="确定要删除吗？" @confirm="() => handleDel(row)">
            <el-button slot="reference" icon="el-icon-delete" link size="mini" class="danger_button">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
      <el-button type="primary" @click="submitForm" size="mini">登 记</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDictItems } from '@/api/sys/dict'
import { archivesRecord, boxRecord } from '@/api/rfid/recordBill'
export default {
  name: 'recordBillDialog',
  data() {
    return {
      type: '',
      dialogTitle: '',
      dialogVisible: false,
      formData: {},
      formDisabled: false,
      formRules: {
        exceptionType: [{ required: true, message: '请选择异常类型', trigger: 'change' }],
      },
      exceptionTypeOptions: [],
      tableData: [],
      tableProps: [],
    }
  },
  created() {
    this.getExceptionType()
  },
  methods: {
    getExceptionType() {
      getDictItems('exception_type').then((res) => {
        if (res.data.code === 200) {
          this.exceptionTypeOptions = res.data.data
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    add(record, type) {
      this.type = type
      if (type === 'box') {
        this.tableProps = [
          { title: '盒号', prop: 'boxNo', width: '', checked: true },
          { title: '盒名', prop: 'boxName', width: '', checked: true },
          { title: 'RFID', prop: 'rfid', width: '', checked: true },
          { title: '存放位置', prop: 'positionId_dictText', width: '', checked: true },
        ]
      } else if (type === 'archive') {
        this.tableProps = [
          { title: '档号', prop: 'archiveNo', width: '', checked: true },
          { title: '题名', prop: 'archiveName', width: '', checked: true },
          { title: 'RFID', prop: 'rfid', width: '', checked: true },
          { title: '存放位置', prop: 'positionId_dictText', width: '', checked: true },
        ]
      }
      this.tableData = record
      this.formDisabled = false
      this.dialogTitle = '添加到登记列表'
      // 先清空表单数据
      this.formData = {}
      this.dialogVisible = true
    },
    handleDel(row) {
      this.tableData = this.tableData.filter((item) => item.labelId !== row.labelId)
    },
    submitForm() {
      this.$refs['vForm'].validate((valid) => {
        if (valid) {
          // 判断表格中是否还有数据
          if (this.tableData.length === 0) {
            this.$message.warning('登记列表不能为空')
            return
          }
          this.formData.labelIds = this.tableData.map((item) => item.labelId)
          if (this.type === 'box') {
            boxRecord(this.formData)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$message.success('登记成功')
                  this.closeDialog()
                  this.$emit('ok', this.type)
                } else {
                  this.$message.warning(res.data.message)
                }
              })
              .catch((err) => {
                this.$message.error(err)
              })
          } else if (this.type === 'archive') {
            archivesRecord(this.formData)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$message.success('登记成功')
                  this.closeDialog()
                  this.$emit('ok', this.type)
                } else {
                  this.$message.warning(res.data.message)
                }
              })
              .catch((err) => {
                this.$message.error(err)
              })
          }
        }
      })
    },
    closeDialog() {
      this.$refs['vForm'].resetFields()
      this.dialogVisible = false
    },
  },
}
</script>

<style scoped></style>
